using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class LibrarianViewReservedBooks : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        lblErrorMsg.Text = "";
    }

    public void gridView_ReservedBooks_PageIndexCanging(object sender, GridViewPageEventArgs e)
    {
        ManageReservedBook mrBook = new ManageReservedBook();
        SqlDataAdapter sqlDat = mrBook.GetReservedBooks_LibrarianView(int.Parse(this.Request.QueryString.Get("id")));
        DataSet d = new DataSet();
        sqlDat.Fill(d);
        gridView_ReservedBooks.DataSource = d;
        gridView_ReservedBooks.PageIndex = e.NewPageIndex;
        gridView_ReservedBooks.DataBind();
    }

    public void gridView_ReadyBooks_PageIndexCanging(object sender, GridViewPageEventArgs e)
    {
        ManageReservedBook mrBook = new ManageReservedBook();
        SqlDataAdapter sqlDat = mrBook.GetReadyBooks_LibrarianView(int.Parse(this.Request.QueryString.Get("id")));
        DataSet d = new DataSet();
        sqlDat.Fill(d);
        gridView_ReadyBooks.DataSource = d;
        gridView_ReadyBooks.PageIndex = e.NewPageIndex;
        gridView_ReadyBooks.DataBind();
    }

    public void gridView_ReservedBooks_Sorting(object sender, GridViewSortEventArgs e)
    {
        DataTable dTable = gridView_ReservedBooks.DataSource as DataTable;

        if (dTable != null)
        {
            try
            {
                DataView dView = new DataView(dTable);
                dView.Sort = e.SortExpression + " " + ConvertSortDirectionToSQL(e.SortDirection);
                gridView_ReservedBooks.DataSource = dView;
                gridView_ReservedBooks.DataBind();
            }
            catch (Exception ex)
            {
                lblErrorMsg.Text = ex.Message.ToString();
            }
        }
    }

    private string ConvertSortDirectionToSQL(SortDirection sortDirection)
    {
        string newSortDirection = string.Empty;
        switch (sortDirection)
        { 
            case SortDirection.Ascending:
                newSortDirection = "ASC";
                break;
            case SortDirection.Descending:
                newSortDirection = "DESC";
                break;
        }
        return newSortDirection;
    }
    
    protected void btnGenerate_Click(object sender, EventArgs e)
    {
        try
        {
            lblErrorMsg.Text = "";

            ManageReservedBook mrBook = new ManageReservedBook();
            SqlDataAdapter sqlDat = mrBook.GetReservedBooks_LibrarianView(int.Parse(this.Request.QueryString.Get("id")));
            DataSet d = new DataSet();
            sqlDat.Fill(d);
            gridView_ReservedBooks.DataSource = d;
            gridView_ReservedBooks.DataBind();

            if (gridView_ReservedBooks.Rows.Count < 1)
            {
                lblErrorMsg.Text = "There are no new reserved books";
            }
        }
        catch (Exception ex)
        {
            lblErrorMsg.Text = ex.Message.ToString();
        }
    }

    protected void btnHideReserved_Click(object sender, EventArgs e)
    {
        try
        {
            lblErrorMsg.Text = "";

            DataTable dt = new DataTable();
            gridView_ReservedBooks.DataSource = dt;
            gridView_ReservedBooks.DataBind();
        }
        catch(Exception ex)
        {
            lblErrorMsg.Text = ex.Message.ToString();
        }
    }

    protected void gridView_ReservedBooks_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            GridViewRow gvRow = gridView_ReservedBooks.SelectedRow;
            int reserveId = int.Parse(gvRow.Cells[0].Text.ToString());
            string title = gvRow.Cells[5].Text.ToString();

            lblErrorMsg.Text = "";

            ManageReservedBook mrBook = new ManageReservedBook();
            int affectedRows = mrBook.UpdateReservedBookStatus_LibrarianView(reserveId, "Ready to Pickup");

            if (affectedRows < 1)
            {
                lblErrorMsg.Text = "There is a problem";
            }
            else
            {
                lblErrorMsg.Text = title + " is ready to pickup";
            }

            SqlDataAdapter sqlDat = mrBook.GetReservedBooks_LibrarianView(int.Parse(this.Request.QueryString.Get("id")));
            DataSet d = new DataSet();
            sqlDat.Fill(d);
            gridView_ReservedBooks.DataSource = d;
            gridView_ReservedBooks.DataBind();
            if (gridView_ReservedBooks.Rows.Count < 1)
            {
                lblErrorMsg.Text = "There are no new reserved books";
            }
        }
        catch (Exception ex)
        {
            lblErrorMsg.Text = ex.Message.ToString();
        }
    }

    protected void gridView_ReadyBooks_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            GridViewRow gvRow = gridView_ReadyBooks.SelectedRow;
            int reserveId = int.Parse(gvRow.Cells[0].Text.ToString());
            string title = gvRow.Cells[5].Text.ToString();

            lblErrorMsg.Text = "";

            ManageReservedBook mrBook = new ManageReservedBook();
            int affectedRows = mrBook.UpdateReservedBookStatus_LibrarianView(reserveId, "Cancelled");

            if (affectedRows < 1)
            {
                lblErrorMsg.Text = "There is a problem";
            }
            else
            {
                lblErrorMsg.Text = title + " has been cancelled";
            }

            SqlDataAdapter sqlDat = mrBook.GetReadyBooks_LibrarianView(int.Parse(this.Request.QueryString.Get("id")));
            DataSet d = new DataSet();
            sqlDat.Fill(d);
            gridView_ReadyBooks.DataSource = d;
            gridView_ReadyBooks.DataBind();
            if (gridView_ReadyBooks.Rows.Count < 1)
            {
                lblErrorMsg.Text = "There are no reserved books being held";
            }
        }
        catch (Exception ex)
        {
            lblErrorMsg.Text = ex.Message.ToString();
        }
    }

    protected void btnViewReady_Click(object sender, EventArgs e)
    {
        try
        {
            lblErrorMsg.Text = "";

            ManageReservedBook mrBook = new ManageReservedBook();
            SqlDataAdapter sqlDat = mrBook.GetReadyBooks_LibrarianView(int.Parse(this.Request.QueryString.Get("id")));
            DataSet d = new DataSet();
            sqlDat.Fill(d);
            gridView_ReadyBooks.DataSource = d;
            gridView_ReadyBooks.DataBind();

            if (gridView_ReadyBooks.Rows.Count < 1)
            {
                lblErrorMsg.Text = "There are no reserved books being held";
            }
        }
        catch (Exception ex)
        {
            lblErrorMsg.Text = ex.Message.ToString();
        }
    }

    protected void btnHideReady_Click(object sender, EventArgs e)
    {
        try
        {
            lblErrorMsg.Text = "";

            DataTable dt = new DataTable();
            gridView_ReadyBooks.DataSource = dt;
            gridView_ReadyBooks.DataBind();
        }
        catch (Exception ex)
        {
            lblErrorMsg.Text = ex.Message.ToString();
        }
    }

    public void chkbxReservedBooks_CheckedChanged(object sender, EventArgs e)
    {
        if (chkbxReservedBooks.Checked)
        {
            try
            {
                lblErrorMsg.Text = "";

                ManageReservedBook mrBook = new ManageReservedBook();
                SqlDataAdapter sqlDat = mrBook.GetReservedBooks_LibrarianView(int.Parse(this.Request.QueryString.Get("id")));
                DataSet d = new DataSet();
                sqlDat.Fill(d);
                gridView_ReservedBooks.DataSource = d;
                gridView_ReservedBooks.DataBind();

                if (gridView_ReservedBooks.Rows.Count < 1)
                {
                    lblErrorMsg.Text = "There are no new reserved books";
                }
            }
            catch (Exception ex)
            {
                lblErrorMsg.Text = ex.Message.ToString();
            }
        }
        else
        {
            try
            {
                lblErrorMsg.Text = "";

                DataTable dt = new DataTable();
                gridView_ReservedBooks.DataSource = dt;
                gridView_ReservedBooks.DataBind();
            }
            catch (Exception ex)
            {
                lblErrorMsg.Text = ex.Message.ToString();
            }
        }
    }

    public void chkbxReadyBooks_CheckedChanged(object sender, EventArgs e)
    {
        if (chkbxReadyBooks.Checked)
        {
            try
            {
                lblErrorMsg.Text = "";

                ManageReservedBook mrBook = new ManageReservedBook();
                SqlDataAdapter sqlDat = mrBook.GetReadyBooks_LibrarianView(int.Parse(this.Request.QueryString.Get("id")));
                DataSet d = new DataSet();
                sqlDat.Fill(d);
                gridView_ReadyBooks.DataSource = d;
                gridView_ReadyBooks.DataBind();

                if (gridView_ReadyBooks.Rows.Count < 1)
                {
                    lblErrorMsg.Text = "There are no reserved books being held";
                }
            }
            catch (Exception ex)
            {
                lblErrorMsg.Text = ex.Message.ToString();
            }
        }
        else
        {
            try
            {
                lblErrorMsg.Text = "";

                DataTable dt = new DataTable();
                gridView_ReadyBooks.DataSource = dt;
                gridView_ReadyBooks.DataBind();
            }
            catch (Exception ex)
            {
                lblErrorMsg.Text = ex.Message.ToString();
            }
        }
    }
}
